﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Logistics_Service.DTO;

namespace Logistics_Service.DAO
{
    public class TuyenDuongDAO
    {
        public TuyenDuongDTO[] DanhSachTuyenDuong()
        {
            TuyenDuongDTO[] arr;
            string query = string.Format("select * from TuyenDuong");
            DataProvider provider = new DataProvider();
            DataTable dt = provider.ExecuteQuery(query);
            int n = dt.Rows.Count;
            arr = new TuyenDuongDTO[n];
            for (int i = 0; i < n; i++)
            {
                object td = GetDataFromDataRow(dt, i);
                arr[i] = (TuyenDuongDTO)td;
            }
            return arr;
        }
        protected object GetDataFromDataRow(DataTable dt, int i)
        {
            TuyenDuongDTO td = new TuyenDuongDTO();
            td.Ma = dt.Rows[i]["Ma"].ToString();
            td.Ten = dt.Rows[i]["Ten"].ToString();
            td.KhoangCach = float.Parse(dt.Rows[i]["KhoangCach"].ToString());
            td.ThoiGianDuKien = float.Parse(dt.Rows[i]["ThoiGianDuKien"].ToString());
            td.ChiPhiDuKien = float.Parse(dt.Rows[i]["ChiPhiDuKien"].ToString());

            td.DiemDau = ChiNhanhDAO.Get(dt.Rows[i]["DiemDau"].ToString());
            td.DiemCuoi = ChiNhanhDAO.Get(dt.Rows[i]["DiemCuoi"].ToString());
            td.DaXoa = bool.Parse(dt.Rows[i]["DaXoa"].ToString());

            return (object)td;
        }
        public float TimSoKm(string diemdau, string diemcuoi)
        {
            float kq = 0;
            string query = string.Format("select KhoangCach from TuyenDuong where DiemDau='{0}' and DiemCuoi='{1}'",diemdau,diemcuoi);
            DataProvider provider = new DataProvider();
            DataTable dt = provider.ExecuteQuery(query);
            if (dt.Rows.Count!=0)
                kq=float.Parse(dt.Rows[0][0].ToString());
            return kq;
        }
    }
}